package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.pojo.Menu;
import com.dk.pojo.TPerson;
import com.dk.service.MenuService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("menu")
public class MenuController {

    //dubbo接口远程调用
    @Reference(timeout = 600000) //注意一定不要写错, 不然空指针
    private MenuService menuService;

    @RequestMapping("treeview")
    public String treeview(){
        return "treeview";
    }

    //查询
    @RequestMapping("getInfo")
    @ResponseBody
    public List<Map<String,Object>> getInfo(HttpServletRequest request){
        //根据用户id查询菜单数
        TPerson person = (TPerson) SecurityUtils.getSubject().getPrincipal();
        List<Map<String,Object>> mapList= menuService.getInfo(person.getPid());
        return mapList;
    }

    @RequestMapping("toShow")
    public String toShow(){
        return "menu/show";
    }

    @RequestMapping("getInfoZtree")
    @ResponseBody
    public List<Map<String,Object>> getInfoZtree() {
        List<Map<String,Object>> result = menuService.getMenuZtree();
        return result;
    }

    //新增
    @RequestMapping("toAdd")
    public String toAdd(){
        return "menu/add";
    }

    @RequestMapping(value = "add")
    @ResponseBody
    public boolean add(Menu menu){
        boolean b = menuService.add(menu);
        return b;
    }
    //修改回显
    @RequestMapping("toUpdate/{mid}")
    public String toUpdate(@PathVariable("mid")Integer mid, Model model){
    Menu menu= menuService.toUpdate(mid);
    model.addAttribute("menu",menu);
        return "menu/update";
    }
    //修改
    @RequestMapping("update")
    @ResponseBody
    public boolean update(Menu menu){
        boolean b=menuService.update(menu);
        return b;
    }
    //删除
    @RequestMapping("deleteById/{mid}")
    @ResponseBody
    public boolean deleteById(@PathVariable("mid")Integer mid) {
        return menuService.deleteById(mid);
    }
    @RequestMapping("getMenuZtreeChecked/{rid}")
    @ResponseBody
    public List<Map<String,Object>> getMenuZtreeChecked(@PathVariable("rid")Integer rid) {
        List<Map<String,Object>> list  = menuService.getMenuZtreeChecked(rid);
        return list;
    }
}
